package response

import (
	"net/http"

	"openzbox.com/frame/echo"
)

type Result struct {
	Success bool        `json:"success"`
	Code    int         `json:"code"` //0 成功 1：失败 2：登录过期
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
}

func response(success bool, code int, msg string, data interface{}, ctx echo.Context) error {
	r := Result{success, code, msg, data}
	return ctx.JSON(http.StatusOK, r)
	// if code == 0 {
	// 	return ctx.JSON(http.StatusOK, r)
	// } else if code == 1 {
	// 	return ctx.JSON(http.StatusBadRequest, r)
	// } else {
	// 	return ctx.JSON(http.StatusUnauthorized, r)
	// }

}

func successResponse(data interface{}, ctx echo.Context) error {
	return response(true, 0, "请求成功", data, ctx)
}

func failResponse(code int, msg string, ctx echo.Context) error {
	return response(false, code, msg, nil, ctx)
}

func SuccessResultWithEmptyData(ctx echo.Context) error {
	return successResponse(nil, ctx)
}

func SuccessResult(data interface{}, ctx echo.Context) error {
	return successResponse(data, ctx)
}

func FailResultWithDefaultMsg(code int, ctx echo.Context) error {
	return failResponse(code, "请求失败", ctx)
}

func FailResult(code int, msg string, ctx echo.Context) error {
	return failResponse(code, msg, ctx)
}
